package com.chezhujie.www.service;

import com.chezhujie.www.domain.Czjorderdistribution;
import com.chezhujie.www.persistence.CzjorderdistributionMapper;
import com.sys.core.exception.ClassCheckException;
import com.sys.core.page.Pagination;
import com.sys.core.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 订单配送表
 * Created by 汤伟杰 on 2017/5/23.
 */
@Service
public class CzjorderdistributionService {
    @Autowired
    private CzjorderdistributionMapper czjorderdistributionMapper;

    public Pagination list(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) throws Exception {

        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        int count = czjorderdistributionMapper.getTotalRows(params);
        List<Map<String, Object>> list = czjorderdistributionMapper.getList(params);
        pagination.setTotalCount(count);
        pagination.setList(list);
        return pagination;
    }


    public void insert(Czjorderdistribution czjorderdistribution){
        if(StringUtils.isNotBlank(czjorderdistribution.getId())){
        czjorderdistribution.setId(UUIDUtils.getUUID());
        }
        czjorderdistributionMapper.insert(czjorderdistribution);
    }

    public void update(Czjorderdistribution czjorderdistribution){
        czjorderdistributionMapper.update(czjorderdistribution);
    }
    public void deleteById(String id){
        czjorderdistributionMapper.deleteById(id);
    }
    public void deleteByIds(List<String> list){
        czjorderdistributionMapper.deleteByIds(list);
    }
    public Czjorderdistribution queryById(String id){
        return czjorderdistributionMapper.queryById(id);
    }
    public List<Czjorderdistribution> queryByIds(List<String> list){
        List<Czjorderdistribution> listCzjorderdistribution = czjorderdistributionMapper.queryByIds(list);
        if(listCzjorderdistribution==null){
            listCzjorderdistribution = new ArrayList<Czjorderdistribution>();
        }
        return listCzjorderdistribution;
    }

    public void checkEntity(Czjorderdistribution czjorderdistribution) throws Exception {
            if(czjorderdistribution.getOrderCode()!=null&&czjorderdistribution.getOrderCode().length()>25){
                throw new ClassCheckException("订单配送表订单编号不能超过25个字符");
            }
            if(czjorderdistribution.getRecipients()!=null&&czjorderdistribution.getRecipients().length()>19){
                throw new ClassCheckException("订单配送表收货人不能超过19个字符");
            }
            if(czjorderdistribution.getAddress()!=null&&czjorderdistribution.getAddress().length()>255){
                throw new ClassCheckException("订单配送表收货地址不能超过255个字符");
            }
            if(czjorderdistribution.getPhoneNo()!=null&&czjorderdistribution.getPhoneNo().length()>50){
                throw new ClassCheckException("订单配送表手机号不能超过50个字符");
            }
            if(czjorderdistribution.getPostalCode()!=null&&czjorderdistribution.getPostalCode().length()>19){
                throw new ClassCheckException("订单配送表邮政编号不能超过19个字符");
            }
            if(czjorderdistribution.getEmail()!=null&&czjorderdistribution.getEmail().length()>50){
                throw new ClassCheckException("订单配送表邮箱不能超过50个字符");
            }
            if(czjorderdistribution.getMessage()!=null&&czjorderdistribution.getMessage().length()>50){
                throw new ClassCheckException("订单配送表买家留言不能超过50个字符");
            }


    }

    public Czjorderdistribution getByOrderno(String orderno) {
        return czjorderdistributionMapper.getByOrderno(orderno);
    }

    public void saves(List<Czjorderdistribution> listdistribution) {
        if(listdistribution!=null&&listdistribution.size()>0){

            czjorderdistributionMapper.saves(listdistribution);
        }
    }

    public void deleteByOrderCode(String orderNo) {
        czjorderdistributionMapper.deleteByOrderCode(orderNo);
    }
}
